package com.atguigu.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.entity.BaseCategoryView;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.SkuInfo;
import com.atguigu.thread.ThreadPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Controller
public class WebSkuDetailController {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private SearchFeignClient serachFeignClient;


    //编写访问的控制器！
    @RequestMapping("{skuId}.html")
    public String getSkuDetail(@PathVariable Long skuId, Model model){
        Map<String, Object> dataMap = new HashMap<>();

        CompletableFuture<SkuInfo> skuCompletableFuture =  CompletableFuture.supplyAsync(() -> {
            //1.根据skuid获取sku的基本信息(skuInfo)
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            dataMap.put("skuInfo", skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        //2.根据skuid获取spu的销售属性
        CompletableFuture<Void> voidCompletableFuture = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
            Long productId = skuInfo.getProductId();
            List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSkuSalePropertyKeyAndValue(productId, skuId);
        });
        //3.根据skuid获取sku的实时价格信息
        CompletableFuture<Void> skuCompletableFuturePrice =  CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            dataMap.put("price",skuPrice);
        },threadPoolExecutor);
//4.根据skuid获取sku的分类信息
        CompletableFuture<Void> categoryView1 = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
            Long category3Id = skuInfo.getCategory3Id();
            BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
            dataMap.put("categoryView", categoryView);
        });
//5.根据spuId获取该spu所对应的所有sku的销售属性值与sku的对应关系
        CompletableFuture<Void> salePropertyValueIdJson = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map salePropertyValueIdMap = productFeignClient.getSkuSalePropertyValueIdMapping(skuInfo.getProductId());
            //把map转换为json
            dataMap.put("salePropertyValueIdJson", JSON.toJSONString(salePropertyValueIdMap));
        });
//
//        //1.根据skuid获取sku的基本信息(skuInfo)
//            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
//            dataMap.put("skuInfo",skuInfo);
//            //2.根据skuid获取spu的销售属性
//            Long productId = skuInfo.getProductId();
//            List<ProductSalePropertyKey> spuSalePropertyList = productFeignClient.getSkuSalePropertyKeyAndValue(productId, skuId);
//            dataMap.put("spuSalePropertyList",spuSalePropertyList);
//            //3.根据skuid获取sku的实时价格信息
//            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
//            dataMap.put("price",skuPrice);
//            //4.根据skuid获取sku的分类信息
//            Long category3Id = skuInfo.getCategory3Id();
//            BaseCategoryView categoryView = productFeignClient.getCategoryView(category3Id);
//            dataMap.put("categoryView",categoryView);
//            //5.根据spuId获取该spu所对应的所有sku的销售属性值与sku的对应关系
//            Map salePropertyValueIdMap = productFeignClient.getSkuSalePropertyValueIdMapping(productId);
//            //把map转换为json
//            dataMap.put("salePropertyValueIdJson",JSON.toJSONString(salePropertyValueIdMap));
        //更新商品热度
        CompletableFuture<Void> hotScoreFuture = CompletableFuture.runAsync(() -> {
            serachFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        CompletableFuture.allOf(skuCompletableFuture,voidCompletableFuture,
                skuCompletableFuturePrice,categoryView1,salePropertyValueIdJson).join();
        model.addAllAttributes(dataMap);
            return "detail/index";
        }

}
